<?php
$loader->requireOnce('controllers/C_Base_Access.class.php');
class C_Access extends C_Base_Access {
	function C_Access($template_mod = "general") {
		parent::Controller();
		$this->setLayout('login');
		$this->template_mod = $template_mod;
	
	}
	
	function login_session_setup(&$user) {
		
		if($this->security->acl_qcheck('usage', false, 'resources', 'role_admin')) {
			header('Location: '.Celini::link('ListFeatures','Admin'));
			die();
		} else if($this->security->acl_qcheck('usage', false, 'resources', 'role_receptionist')) {				
			header('Location: '.Celini::link('Register', 'OdonReception'));
			die();
		} else if( $this->security->acl_qcheck('usage', false, 'resources', 'role_doctor')) {
			header('Location: '.Celini::link('ViewWorkspace', 'Doctor'));
			die();								
		} else if($this->security->acl_qcheck('usage', false, 'resources', 'role_billing')) {
			//header('Location: '.Celini::link('cashierQueue', 'Queue'));
			header('Location: '.Celini::link('ViewPayment', 'OutPayment'));
			die();
		} else if($this->security->acl_qcheck('usage', false, 'resources', 'role_manager')) {
			header('Location: '.Celini::link('viewDashboard','ManagerDashboard'));
			die();
		} else if($this->security->acl_qcheck('usage', false, 'resources', 'role_accountant')) {
			header('Location: '.Celini::link('ViewAccountingReport', 'AccountingReport'));
			die();
		} else if($this->security->acl_qcheck('usage', false, 'resources', 'marketting_report')) {
			header('Location: '.Celini::link('viewMarkettingReport','ManagementReport'));
			die();
		}
	}
	
	function sLogin_action() {
		$_SESSION['sLogin'] = true;
		$this->view->assign("languageList",L10nHelper::getSupportedLocales());
		$this->view->assign("chosenLanguage",L10nHelper::getLocale());		
		
		$this->assign('FORM_ACTION',Celini::link('login','access'));
		$this->assign('FORGOT_ACTION',Celini::link('forgot','access'));		
		return $this->view->render('slogin.html');	
	}
	
	function sLogin_action_process($redirect = '') {
		if ($_POST['process'] !== "true") {
			return;
		}
		
		$status = false;		
		if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['department'])) {					
			$status = $this->login($_POST['username'], $_POST['password']);		
				
		}
		
		if ($status) {
			ClniAudit::logAccessAttempt($_POST['username'],true);
			if (isset($GLOBALS['config']['maintenanceMode']) &&
				$GLOBALS['config']['maintenanceMode'] === 'splash' &&
				!isset($_SESSION['clicked_through_splash']) &&
				!$_SESSION['clicked_through_splash'] == true
				&& !isset($_GET['nosplash'])) {

				header ("Location: ".Celini::link('splash','Access'));
				exit;
			}
			if ($redirect == '') { $redirect = Celini::link('default','Access'); }

			header ("Location: ". $redirect);
			
			exit;
		}
		else {
			ClniAudit::logAccessAttempt($_POST['username'],false);
			$this->messages->addMessage(L10nHelper::_("Invalid username or password , please try again."));
			if (!isset($_POST['username'])) $_POST['username'] = "";
			$this->assign("username",$_POST['username']);
				
			return $this->sDenied_action();
		}
	}
	
	function sDenied_action() {
		$this->assign('url', 'index.php/main/access/slogin');
		return $this->view->render('denied.html');	
	}
	
	function login_action() {
		$this->view->assign("languageList",L10nHelper::getSupportedLocales());
		$this->view->assign("chosenLanguage",L10nHelper::getLocale());		
		return C_Base_Access::login_action();
		
	}	
}

?>